#pragma once
#include<condition_variable>
#include<iostream>
#include<thread>
#include<mutex>

namespace TestCondition
{
	void test()
	{
		std::condition_variable con_1, con_2;
		std::mutex lock;
		int n = 10000;
		bool flag = true;

		auto fn_1 = [&]()->void
			{
				int num = 0;
				while (num <= n)
				{
					std::unique_lock<std::mutex> lock_guard(lock);
					while (!flag)
					{
						con_1.wait(lock_guard);
					}

					if (num <= n)
						std::cout << num << std::endl;

					flag = false;
					num += 2;
					con_2.notify_one();
				}
			};

		auto fn_2 = [&]()->void
			{
				int num = 1;
				while (num < n)
				{
					std::unique_lock<std::mutex> lock_guard(lock);
					while (flag)
					{
						con_2.wait(lock_guard);
					}

					if (num < n)
						std::cout << num << std::endl;
					flag = true;
					num += 2;
					con_1.notify_one();
				}
			};

		std::thread thread1(fn_1);
		std::thread thread2(fn_2);

		thread1.join();
		thread2.join();

	}
}
